package org.yzh.scheduler.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.yzh.commons.model.APIResult;
import org.yzh.scheduler.model.entity.JobLogDO;
import org.yzh.scheduler.model.entity.ScheduleJobDO;
import org.yzh.scheduler.service.ScheduleJobService;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "任务调度API")
@RequestMapping("api/job")
@RestController
public class ScheduleJobController {

    @Autowired
    private ScheduleJobService scheduleJobService;

    @Operation(summary = "日志列表")
    @GetMapping("log")
    public APIResult<List<JobLogDO>> log(@RequestParam(required = false) String name,
                                         @RequestParam(required = false) String group,
                                         @RequestParam(required = false) LocalDateTime startTime,
                                         @RequestParam(required = false) LocalDateTime endTime) {
        List<JobLogDO> list = scheduleJobService.findLog(name, group, startTime, endTime, 100);
        return APIResult.ok(list);
    }

    @Operation(summary = "分组列表")
    @GetMapping("group")
    public APIResult<String[]> group() {
        String[] result = scheduleJobService.groupNames();
        return APIResult.ok(result);
    }

    @Operation(summary = "任务列表")
    @GetMapping
    public APIResult<List<ScheduleJobDO>> list(@RequestParam(required = false) String name,
                                               @RequestParam(required = false) String group,
                                               @RequestParam(required = false) Boolean disabled) throws SchedulerException {
        List<ScheduleJobDO> list = scheduleJobService.find(name, group, disabled);
        return APIResult.ok(list);
    }

    @Operation(summary = "新增任务")
    @PostMapping
    public APIResult add(@Validated @RequestBody ScheduleJobDO job) throws SchedulerException {
        scheduleJobService.add(job);
        return APIResult.SUCCESS;
    }

    @Operation(summary = "保存任务")
    @PutMapping
    public APIResult update(@Validated @RequestBody ScheduleJobDO job) throws SchedulerException {
        scheduleJobService.update(job);
        return APIResult.SUCCESS;
    }

    @Operation(summary = "删除任务")
    @DeleteMapping
    public APIResult<Boolean> delete(@RequestParam String name, @RequestParam String group) throws SchedulerException {
        scheduleJobService.delete(name, group);
        return APIResult.SUCCESS;
    }

    @Operation(summary = "运行一次")
    @PostMapping("run-once")
    public APIResult<Boolean> runOnce(@RequestParam String name, @RequestParam String group) throws SchedulerException {
        scheduleJobService.runOnce(name, group);
        return APIResult.SUCCESS;
    }

    @Operation(summary = "暂停")
    @PostMapping("pause")
    public APIResult<Boolean> pause(@RequestParam String name, @RequestParam String group) throws SchedulerException {
        scheduleJobService.pause(name, group);
        return APIResult.SUCCESS;
    }

    @Operation(summary = "恢复")
    @PostMapping("resume")
    public APIResult<Boolean> resume(@RequestParam String name, @RequestParam String group) throws SchedulerException {
        scheduleJobService.resume(name, group);
        return APIResult.SUCCESS;
    }
}